//index.js
//获取应用实例
const app = getApp()
let api = require('../../api/index.js')

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function (options) {

    if (options.id== 1){
      api.venue({}, res => {
        wx.getSetting({
          success: res => {
            if (res.authSetting['scope.userInfo']) {
              //console.log('用户已授权')
              wx.switchTab({
                url: '/pages/index/index/index',
              })
              wx.getUserInfo({
                success: response => {
                  // 可以将 res 发送给后台解码出 unionId
                  let data = { // 用户信息获取成功后得到用户头像昵称
                    nickName: response.userInfo.nickName,
                    avatarUrl: response.userInfo.avatarUrl
                  }
                  this.setRegistToken(data)
                  //console.log( response)

                }
              })

            } else {
              console.log('用户未授权')

            }
          }
        })
      })
    }else{
      api.venue({}, res => {
        console.log(res)
        if (res.data.code == 50002) {
          //console.log('token失效')
          return;
        } else {
          // 获取用户信息
          wx.getSetting({
            success: res => {
              if (res.authSetting['scope.userInfo']) {
                //console.log('用户已授权')
                wx.switchTab({
                  url: '/pages/index/index/index',
                })
                wx.getUserInfo({
                  success: response => {
                    // 可以将 res 发送给后台解码出 unionId
                    let data = { // 用户信息获取成功后得到用户头像昵称
                      nickName: response.userInfo.nickName,
                      avatarUrl: response.userInfo.avatarUrl
                    }
                    this.setRegistToken(data)
                    //console.log( response)

                  }
                })

              } else {
                console.log('用户未授权')

              }
            }
          })
        }
      })
    }
    
    
  },
  getUserInfo: function(e) {
    let _this = this;
    if (e.detail.userInfo){
      console.log('用户同意授权')
      app.globalData.userInfo = e.detail.userInfo
      App.zhuge.track('用户同意授权', {});

      let data = { // 用户信息获取成功后得到用户头像昵称
        nickName: e.detail.userInfo.nickName,
        avatarUrl: e.detail.userInfo.avatarUrl
      }
      _this.setRegistToken(data)
      
    }else {
      console.log('用户拒绝授权')
      App.zhuge.track('微信头像昵称授权拒绝', {});
      
      _this.userShowModel();
    }
    
  },
  userShowModel: function(){
    let _this = this;
    wx.showModal({ // 显示提示框
      title: '提示',
      content: '亲，授权用户信息才能正常使用小程序哦',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          wx.openSetting({ // 打开设置页面
            success: (res) => {
              if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录

                wx.getUserInfo({
                  success: res => {
                    let data = res.userInfo;
                    //console.log(data)
                    _this.setRegistToken(data)
                  }
                })
               
              } else { // 如果用户拒绝， 重复调用请求按钮，直到同意为止
                _this.userShowModel();
              }
            }
          })

        }
      }
    })
  },
  setRegistToken: function(data){
    
    app.loginCallback(function (openid){
      let resData = {
        user_name: data.nickName,
        user_img: data.avatarUrl,
        openid_wechat: openid.openid
      }
  
      api.register(resData, res => { // 执行注册 将用户数据提交后台换取token
      //console.log(res)

        App.zhuge.identify([openid.openid], {
          '用户名': resData.user_name
        });
        wx.setStorageSync('token', {
          "token": res.data.result.token,
          "openid": openid,
          "session_key": openid.session_key,
          "user_mobile": res.data.result.user_mobile
        }); // 将token存储到本地存储中
        //console.log(wx.getStorageSync("token")) 
        wx.switchTab({ // 用户同意  从默认页跳转到tab首页
          url: "/pages/index/index/index",
        })
      })
    })
  }
})
